package dyy.zc.alumnibook.ui.fragment;


import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

import dyy.zc.alumnibook.R;
import dyy.zc.alumnibook.base.BaseFragment;
import dyy.zc.alumnibook.bean.FriendCircle;
import dyy.zc.alumnibook.ui.activity.PublishActivity;
import dyy.zc.alumnibook.ui.adapter.FriendCircleAdapter;
import dyy.zc.alumnibook.utils.BmobUtils;
import dyy.zc.alumnibook.utils.Utils;
import dyy.zc.alumnibook.views.EmptyView;


/**
 * 我的朋友圈
 * Created by chuan on 2017/4/12.
 */

public class FriendCircleFragment extends BaseFragment implements View.OnClickListener {
    private final int LIST_SUCCESS = 1;
    public List<FriendCircle> list = new ArrayList<>();
    private View view;
    private TextView title,publish;
    private BmobUtils bmobUtils;
    private PullToRefreshListView pfl;
    private ListView listView;
    private FriendCircleAdapter adapter;
    private EmptyView emptyView;
    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case LIST_SUCCESS:
                    //Bundle bundle = msg.getData();
                    //list.clear();
                    //List<FriendCircle> friendCircles = (List<FriendCircle>) bundle.getSerializable("list");
                    //list.addAll(friendCircles);
                    changeData();
                    break;
            }
        }


    };

    @Override
    protected View onCreateViews(LayoutInflater inflater, ViewGroup container) {
        view = inflater.inflate(R.layout.fragment_friend_circle,container,false);
        findViewsByIds(view);
        bind();
        return view;
    }

    private void bind() {
        Utils.findViewsById(view,R.id.top_rl_right).setOnClickListener(this);
        adapter = new FriendCircleAdapter(getActivity(), list, getVmWidth());
        listView.setAdapter(adapter);
        bmobUtils.queryFriendCircle(LIST_SUCCESS, handler);
        pfl.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                bmobUtils.queryFriendCircle(LIST_SUCCESS,handler);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });
    }

    /**
     * 添加数据
     */
    private void changeData() {
        //判断如果没有数据的话，则显示空提示
        if (list.size() == 0) {
            pfl.setVisibility(View.GONE);
            emptyView.setNotify("暂无动态");
        } else {
            emptyView.setEmptyViewGone();
            pfl.setVisibility(View.VISIBLE);
        }

        adapter.setList(list);
        adapter.notifyDataSetChanged();
        pfl.onRefreshComplete();
    }

    private void findViewsByIds(View view) {
        pfl = Utils.findViewsById(view,R.id.recyclerView);
        pfl.setMode(PullToRefreshBase.Mode.PULL_DOWN_TO_REFRESH);
        listView = pfl.getRefreshableView();

        emptyView = new EmptyView(view);
        title = Utils.findViewsById(view,R.id.top_tv_title);
        publish = Utils.findViewsById(view,R.id.top_tv_right);
        publish.setVisibility(View.VISIBLE);
        publish.setText("发布");
        title.setText("我的朋友圈");
        Utils.findViewsById(view,R.id.top_rl_back).setVisibility(View.GONE);
        bmobUtils = new BmobUtils(getActivity());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.top_rl_right:
                Intent intent = new Intent(getActivity(),PublishActivity.class);
                startActivityForResult(intent,456);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case 456:
                bmobUtils.queryFriendCircle(LIST_SUCCESS, handler);
                break;
        }
    }
}
